package com.hollycrm.unicom.sheet.business.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hollycrm.springframework.web.struts.DispatchActionSupport;
import com.hollycrm.unicom.sheet.BeanResource;
import com.hollycrm.unicom.sheet.code.CodeManager;
import com.hollycrm.unicom.sheet.flowsupport.FlowSupportManager;
import com.hollycrm.unicom.sheet.code.CleanCacheCodeJob;

/**
 * ProblemTreeAction
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class ShowTreeAction
    extends DispatchActionSupport {

  /**
   * execProblem
   *
   * @param mapping ActionMapping
   * @param actionForm ActionForm
   * @param request HttpServletRequest
   * @param response HttpServletResponse
   * @throws Exception
   * @return ActionForward
   */
  public ActionForward execProblem(ActionMapping mapping,
                                   ActionForm actionForm,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws
      Exception {
    String problemTreeID = request.getParameter("treeType");

    CodeManager codeManager = (CodeManager) getBean(BeanResource.CODE_MANAGER);
    List treeList = codeManager.getProblemClassTreeList(problemTreeID);
    request.setAttribute("treeList", treeList);
    return mapping.findForward("problemTree");
  }

  /**
   * execFlowDept
   *
   * @param mapping ActionMapping
   * @param actionForm ActionForm
   * @param request HttpServletRequest
   * @param response HttpServletResponse
   * @throws Exception
   * @return ActionForward
   */
  public ActionForward execFlowDept(ActionMapping mapping,
                                    ActionForm actionForm,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws
      Exception {
    String flowID = request.getParameter("flowID");
    String deptID = request.getParameter("deptID");
    String operateType = request.getParameter("operateType");

    FlowSupportManager fsm = (FlowSupportManager) getBean(BeanResource.
        FLOWSUPPORT_MANAGER);
    List deptList = fsm.getPostDeptList(flowID, deptID, operateType);
    request.setAttribute("deptList", deptList);
    return mapping.findForward("deptList");
  }

  /**
   * execProblemSpell
   *
   * @param mapping ActionMapping
   * @param actionForm ActionForm
   * @param request HttpServletRequest
   * @param response HttpServletResponse
   * @throws Exception
   * @return ActionForward
   */
  public ActionForward execProblemSpell(ActionMapping mapping,
                                        ActionForm actionForm,
                                        HttpServletRequest request,
                                        HttpServletResponse response
                                        ) throws Exception {
    String spellStr = request.getParameter("spellStr");
    spellStr = spellStr.toUpperCase();

    CodeManager codeManager = (CodeManager) getBean(BeanResource.CODE_MANAGER);
    List treeList = codeManager.getProblemClassTreeListBySpell(spellStr);
    request.setAttribute("treeList", treeList);
    return mapping.findForward("problemList");
  }

  /* execFlushCodeCache
  *
  * @param mapping ActionMapping
  * @param actionForm ActionForm
  * @param request HttpServletRequest
  * @param response HttpServletResponse
  * @throws Exception
  * @return ActionForward
  */
 public ActionForward execFlushCodeCache(ActionMapping mapping,
                                       ActionForm actionForm,
                                       HttpServletRequest request,
                                       HttpServletResponse response
                                       ) throws Exception {
   String regionName = request.getParameter("regionName");
   String forward="commited";
   CleanCacheCodeJob job = new CleanCacheCodeJob();
   if(regionName!=null)
   try{
     job.cleanSeperateCache(regionName);
   }catch(Exception e){
     e.printStackTrace();
     forward="errors";
   }
   return mapping.findForward(forward);
 }


}